package com.icss.web.back.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.icss.common.controller.BaseController;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.orders.service.IOrdersDbService;
import com.icss.orders.vo.ReOrders;
import com.icss.orders.vo.ReOrdersGoods;

/**
 * 订单操作
 * 
 * @author 刘鑫
 */
@Controller
@RequestMapping("/back/orders/")
public class OrdersBackController extends BaseController
{
	@Resource
	private IOrdersDbService ordersDbService;
	
	/**
	 * 订单列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/ordersList")
	public String ordersList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ReOrders> apiResponse = this.ordersDbService.findCondListOrdersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/back/orders/ordersList";
	}

	/**
	 * 订单更新
	 * @param request
	 * @param response
	 */
	@RequestMapping("/ordersUpdate")
	public String ordersUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ReOrders> apiResponse = this.ordersDbService.findOneOrdersService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/back/orders/ordersUpdate";
		}
		/* 页面跳转 */
		return "/back/orders/ordersInfo";
	}

	/**
	 * 订单商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/ordersGoodsList")
	public String ordersGoodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String ordersId = request.getParameter("ordersId");
		if(ordersId == null)
		{
			ordersId = "" ; 
		}
		request.setAttribute("ordersId", ordersId);
		condMap.put("ordersId", ordersId);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ReOrdersGoods> apiResponse = this.ordersDbService.findCondListOrdersGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/back/orders/ordersGoodsList";
	}

	/**
	 * 订单商品更新
	 * @param request
	 * @param response
	 */
	@RequestMapping("/ordersGoodsUpdate")
	public String ordersGoodsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ReOrdersGoods> apiResponse = this.ordersDbService.findOneOrdersGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/back/orders/ordersGoodsUpdate";
		}
		/* 页面跳转 */
		return "/back/orders/ordersGoodsInfo";
	}
}